package com.aplink.generic.util.convertcount;

import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropCol;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.ObjPropValue;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.objectrule.ActionBaseRule;
import com.aplink.generic.util.objectrule.ActionCompareRule;
import com.aplink.generic.util.objectrule.TargetRule;
import com.aplink.generic.util.objectrule.TargetRuleCollection;

import java.util.ArrayList;

public class GenericFilter {

    public static boolean checkAttributedInsideContentData( String attributeID,
                                                            String tableName, ArrayList<ObjPropInfo> objPropInfoList ) {

        boolean isInside = false;

        for ( int i = 0 ; i < objPropInfoList.size() ; i++ ) {
            final ObjPropInfo objPropInfo = objPropInfoList.get( i );

            if ( ( objPropInfo.getObjectMapping().compareToIgnoreCase( tableName ) == 0 )
                    && ( objPropInfo.getObjectProperty().compareToIgnoreCase(
                    attributeID ) == 0 ) ) {
                isInside = true;
            }
        }
        return isInside;
    }

    public static boolean checkAttributeInsideObjPropInfoList(
            String attributeId, ArrayList<ObjPropInfo> arrMainObjPropInfo ) {

        for ( int i = 0 ; i < arrMainObjPropInfo.size() ; i++ ) {
            final ObjPropInfo objPropInfo = arrMainObjPropInfo.get( i );
            if ( attributeId
                    .compareToIgnoreCase( objPropInfo.getObjectProperty() ) == 0 ) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkUniqueTableNameObjPropInfoList(
            ArrayList<ObjPropInfo> objPropInfoList, String tableName ) {
        int tempcount = 0;
        if ( objPropInfoList.size() == 0 ) {
            return false;
        }

        for ( int i = 0 ; i < objPropInfoList.size() ; i++ ) {
            if ( tableName.compareToIgnoreCase( objPropInfoList.get( i )
                    .getObjectMapping() ) == 0 ) {
                tempcount++;
            }
        }

        if ( tempcount == objPropInfoList.size() ) {
            return true;
        }

        return false;
    }

    public static String[] getArrayValueFromAnAttribute(
            ArrayList<GenericObject> arrObjectList, String textAttribute ) {

        final String[] arrValue = new String[arrObjectList.size()];

        for ( int i = 0 ; i < arrObjectList.size() ; i++ ) {
            // we get the current GenericObject
            // each attribute <name, value> correspond to each attribute of
            // GenericObject
            ArrayList<GenericAttribute<?, ?>> attributeList = new ArrayList<GenericAttribute<?, ?>>();
            attributeList = arrObjectList.get( i ).getAttributeList();
            int j = 0;

            String key = "";

            while ( j < attributeList.size() ) {
                final String attributeName = String.valueOf( attributeList
                        .get( j ).getName() );
                if ( attributeName.compareToIgnoreCase( textAttribute ) == 0 ) {
                    key = String.valueOf( attributeList.get( j ).getValue() );
                }

                j++;
            }

            arrValue[i] = key;
        }

        return arrValue;
    }

    public static SqlDataType getDataTypeOfAttributeFromObjectPropMappingName(
            ArrayList<ObjPropInfo> objPropList, String attributeID ) {
        for ( int i = 0 ; i < objPropList.size() ; i++ ) {
            final ObjPropInfo objPropInfo = objPropList.get( i );
            final String name = objPropInfo.getObjectProperty();
            if ( name.compareTo( attributeID ) == 0 ) {
                return objPropInfo.getDataType();
            }
        }
        return null;
    }

    public static ObjPropInfo getKeyAttributeFromObjPropInfoList(
            ArrayList<ObjPropInfo> objPropInfoList ) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for ( int i = 0 ; i < objPropInfoList.size() ; i++ ) {
            // find attributeKey
            objPropInfo = objPropInfoList.get( i );
            if ( objPropInfo.isKey() ) {
                return objPropInfo;
            }
        }

        return null;
    }

    public static ObjPropInfo getObjPropInfoOfAccountId(
            ArrayList<ObjPropInfo> mObjectPropInfo ) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for ( int i = 0 ; i < mObjectPropInfo.size() ; i++ ) {
            // find attributeKey
            objPropInfo = mObjectPropInfo.get( i );
            if ( objPropInfo.isAccountId() == true ) {
                return objPropInfo;
            }
        }
        return null;
    }

    public static ObjPropInfo getObjPropInfoOfSort(
            ArrayList<ObjPropInfo> mObjectPropInfo ) {
        ObjPropInfo objPropInfo = new ObjPropInfo();
        for ( int i = 0 ; i < mObjectPropInfo.size() ; i++ ) {
            objPropInfo = mObjectPropInfo.get( i );
            if ( objPropInfo.isSort() ) {
                return objPropInfo;
            }
        }
        return null;
    }

    public static ObjPropInfo getObjPropInfoOfAccountPass(
            ArrayList<ObjPropInfo> mObjectPropInfo ) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for ( int i = 0 ; i < mObjectPropInfo.size() ; i++ ) {
            // find attributeKey
            objPropInfo = mObjectPropInfo.get( i );
            if ( objPropInfo.isAccountPass() == true ) {
                return objPropInfo;
            }
        }

        return null;
    }

    public static ArrayList<ObjPropInfo> getObjPropListMainTable(
            String tableName, ArrayList<ObjPropInfo> totalObjPropList ) {
        final ArrayList<ObjPropInfo> mainObjPropList = new ArrayList<ObjPropInfo>();

        for ( int i = 0 ; i < totalObjPropList.size() ; i++ ) {
            final ObjPropInfo objPropInfo = totalObjPropList.get( i );

            if ( objPropInfo.getObjectMapping().compareToIgnoreCase( tableName ) == 0 ) {
                mainObjPropList.add( objPropInfo );
            }
        }
        return mainObjPropList;
    }

    // CodeRV: 15 Sep 2013 - Add support for ObjPropCol
    public static ObjPropCol getObjPropListMainTable( String tableName,
                                                      ObjPropCol totalObjPropList ) {
        ObjPropCol mainObjPropList = new ObjPropCol();

        for ( int i = 0 ; i < totalObjPropList.size() ; i++ ) {
            ObjPropInfo objPropInfo = (ObjPropInfo) totalObjPropList.get( i );
            if ( objPropInfo.getPrimaryObjectMapping() != null ) {
                if ( objPropInfo.isFKey()
                        && objPropInfo.getPrimaryObjectMapping()
                        .equalsIgnoreCase( tableName ) ) {
                    mainObjPropList.add( objPropInfo );
                }
            }
            if ( objPropInfo.getObjectMapping().equalsIgnoreCase( tableName ) ) {
                mainObjPropList.add( objPropInfo );
            }
        }
        return mainObjPropList;
    }

    public static ArrayList<ArrayList<ObjPropInfo>> getObjPropListParentTable(
            String tableName, ArrayList<ObjPropInfo> totalObjPropList ) {

        final ArrayList<ArrayList<ObjPropInfo>> parentObjPropList = new ArrayList<ArrayList<ObjPropInfo>>();

        for ( int i = 0 ; i < totalObjPropList.size() ; i++ ) {
            final ObjPropInfo objPropInfo = totalObjPropList.get( i );
            ArrayList<ObjPropInfo> currentParentObjPropList = new ArrayList<ObjPropInfo>();
            ;

            if ( objPropInfo.getObjectMapping().compareToIgnoreCase( tableName ) != 0 ) {
                currentParentObjPropList = getObjPropListMainTable(
                        objPropInfo.getObjectMapping(), totalObjPropList );

            }
            if ( ( currentParentObjPropList.size() != 0 )
                    && ! parentObjPropList.contains( currentParentObjPropList ) ) {
                parentObjPropList.add( currentParentObjPropList );
            }
        }
        return parentObjPropList;
    }

    public static ObjPropValue getObjPropValueFromControlSpecList(
            ArrayList<ObjPropValue> objPropValueList, ControlSpec controlSpec ) {

        if ( controlSpec.getControlId() == null ) {
            return null;
        }

        ObjPropValue objPropvalue = new ObjPropValue();

        for ( int i = 0 ; i < objPropValueList.size() ; i++ ) {
            objPropvalue = objPropValueList.get( i );

            if ( ( objPropvalue.getObjectPropID().compareTo( "" ) == 0 )
                    && ( controlSpec.getMappingName().compareTo( "" ) == 0 ) ) {
                return null;
            }
            if ( objPropvalue.getObjectPropID().compareToIgnoreCase(
                    controlSpec.getMappingName() ) == 0 ) {
                return objPropvalue;
            }

        }

        return null;
    }

    public static ArrayList<GenericObject> getSubGenericObjectListFromAnAttribute(
            ArrayList<GenericObject> genericObjectList, String attributeTag,
            String attributeTagValue ) {

        if ( attributeTagValue == "" ) {
            return genericObjectList;
        }
        final ArrayList<GenericObject> subGenericObjectList = new ArrayList<GenericObject>();

        for ( int i = 0 ; i < genericObjectList.size() ; i++ ) {

            final GenericObject genericObject = genericObjectList.get( i );
            int attributeIndex = 0;
            while ( attributeIndex < genericObject.getAttributeList().size() ) {
                final String attributeName = String.valueOf( genericObject
                        .getAttributeList().get( attributeIndex ).getName() );
                final String attributeValue = String.valueOf( genericObject
                        .getAttributeList().get( attributeIndex ).getValue() );
                if ( ( attributeName.compareToIgnoreCase( attributeTag ) == 0 )
                        && ( attributeValue
                        .compareToIgnoreCase( attributeTagValue ) == 0 ) ) {
                    subGenericObjectList.add( genericObject );
                    break;
                }
                attributeIndex++;
            }
        }

        return subGenericObjectList;
    }

    public static String getTableNameFromObjPropInfoListAtKeyAtt(
            ArrayList<ObjPropInfo> objPropInfoList, String tableName ) {

        // we should make sure that whole the items in this ObjPropInfo list
        // must be the same Om_ID
        final boolean isUnique = checkUniqueTableNameObjPropInfoList(
                objPropInfoList, tableName );
        if ( isUnique == false ) {
            return "";
        }

        ObjPropInfo objPropInfoKey = new ObjPropInfo();
        objPropInfoKey = getKeyAttributeFromObjPropInfoList( objPropInfoList );
        if ( objPropInfoKey == null ) {
            return "";
        }

        return objPropInfoKey.getObjectMapping();

    }

    public static String getTargetActivityFromRule(
            TargetRuleCollection targetCollection,
            GenericObject passedGenericObject ) {

        if ( targetCollection == null ) {
            return null;
        }
        final ArrayList<TargetRule> arrTargetRules = targetCollection
                .getArrTargetRules();
        for ( int i = 0 ; i < arrTargetRules.size() ; i++ ) {
            final TargetRule currentRule = arrTargetRules.get( i );
            final ActionBaseRule absRule = currentRule.getActionBaseRule();
            if ( absRule instanceof ActionCompareRule ) {
                final boolean isSatisfied = absRule
                        .isSatisfiedActionRule( passedGenericObject );
                if ( isSatisfied == true ) {
                    return currentRule.getTargetActivity();
                }
            }

        }

        return null;
    }

}
